/*
	Software License Agreement (BSD License) 
	Copyright (c) 2008, OMNESOFT, L.L.C.
	All rights reserved.
	
	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
	conditions are met:
	
	Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
	Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
	in the documentation and/or other materials provided with the distribution.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
	NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
	THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
	OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.omnesoft.plugin.qb;

import com.servoy.j2db.util.Debug;

import ibizqb.Account; // Just kinda picked a class to use its qbconnection methods
import ibizqb.IBizQBException;

public class QBConnection {

	public String connectionString = null;
	public Integer connectionMode = null;
	
	final private Account account = new Account(); // We want to always use this same instance of Account
	
	public QBConnection() {		
		this.connectionString = this.getConnectionString();
		this.connectionMode = this.getConnectionMode();
	}
	
	public Boolean open(String connectionString, int connectionMode, Boolean useSSL, String sslCertPath) throws IBizQBException {
		try { 			
			// TODO: Confirm the order these initializations should happen in
			this.setQBConnectionMode(connectionMode);
			this.setQBConnectionString(connectionString, useSSL, sslCertPath);
			
			this.connectionMode = connectionMode;
			
			
			this.account.openQBConnection();
			
			return true;
		} catch (IBizQBException e) {
			Debug.error(e);
			
			// TODO: Should the QB connection be closed here manually?
			try {
				this.close();
			} catch (IBizQBException ex) {
				Debug.error(ex); // More of a silent fail here
			}
			
			throw(e); // Throws back to the provider to allow Servoy developers to try catch in JS
		}
	}
	
	public Boolean close() throws IBizQBException {
		try {
			this.account.closeQBConnection();
			
			// Do I have to do this manually? I don't think so, I think close will do it for me?
//			this.connectionString = null;
//			this.connectionMode = null;
			
			return true;
		} catch (IBizQBException e) {
			Debug.error(e);
			
			throw(e);
		}
	}
	
	private String getConnectionString() {
		return this.account.getQBConnectionString();
	}
	
	private Integer getConnectionMode() {
		return this.account.getQBConnectionMode();
	}
	
	private Boolean setQBConnectionString(String connectionString, Boolean useSSL, String sslCertPath) throws IBizQBException {
		
		// If using SSL, connection strings will be https, so I think we need to establish ssl first
		if(useSSL) {
			try {
				this.establishSSLConnection(sslCertPath);
			} catch(IBizQBException e) {
				Debug.error(e);
				
				throw(e);
			}
		}
		
		try {
			this.account.setQBConnectionString(connectionString);
			this.connectionString = connectionString;
			return true;
		} catch (IBizQBException e) {
			Debug.error(e);
			
			throw(e);
		}
	}
	
	private Boolean setQBConnectionMode(Integer connectionMode) throws IBizQBException {
		try {
			this.account.setQBConnectionMode(connectionMode);
			this.connectionMode = connectionMode;
			
			return true;
		} catch (IBizQBException e) {
			Debug.error(e);
			
			throw(e);
		}
	}
	
	// See: http://www.nsoftware.com/ibiz/quickbooks/connector/
	private Boolean establishSSLConnection(String sslCertPath) throws IBizQBException {
		try {
			// Not sure what the path would be in smart client or web client??? Maybe media:///..., maybe http://...
			this.account.config("SSLAcceptServerCertFile=" + sslCertPath); // e.g. c:\exported_cert.cer"
			
			return true;
		} catch (IBizQBException e) {
			Debug.error(e);
			
			throw(e);
		}
	}
}
